package com.blue.base.oauth.resource.user.config;

import com.blue.base.oauth.common.config.ResourceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * 资源服务器核心配置
 *
 * @author liulei
 * @version 1.0
 */
@Configuration
@EnableResourceServer// 所有请求此服务的api，都将会去header中寻找token，没有则不让通过
public class ResourceServiceAdapter extends ResourceServerConfigurerAdapter {

    @Autowired
    private ResourceConfig resourceConfig;

    @Autowired
    private TokenStore tokenStore;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //所有请求默认都不做认证
        http.authorizeRequests()
                .antMatchers("/user/**").authenticated()
                .anyRequest().permitAll();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(resourceConfig.getResourceId())
                .tokenStore(tokenStore)
                .stateless(true);
    }

}
